package com.sky.service.admin.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.admin.SetmealDishMapper;
import com.sky.mapper.admin.SetmealMapper;
import com.sky.pojo.Dish;
import com.sky.pojo.Setmeal;
import com.sky.pojo.SetmealDish;
import com.sky.result.PageResult;
import com.sky.service.admin.SetmealService;

import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * 套餐业务逻辑处理层
 */
@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;


    /**
     * 新增套餐
     *
     * @param dto
     */
    @Override
    @Transactional
    public void add(SetmealDTO dto) {
        log.info("请求套餐中的数据{}", dto);

        //拷贝保存
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(dto, setmeal);
        log.info("业务层：添加套餐{}", dto);
        log.info("拷贝后的套餐实例对象{}", setmeal);

        setmeal.setStatus(StatusConstant.DISABLE);//设置菜品默认禁用
        log.info("售卖状态{}", setmeal.getStatus());

        //1.新增套餐
        setmealMapper.addGetId(dto);
        log.info("填充后的套餐{}", setmeal);

        //插入套餐关系 获取关系主键
        List<SetmealDish> setmealDishes = new SetmealDTO().getSetmealDishes();
        if (!CollectionUtils.isEmpty(setmealDishes)) {
            for (SetmealDish setmealDish : setmealDishes) {
                log.info("拿到套餐关系中的每组对象数据{}", setmealDishes);

                setmealDish.setSetmealId(dto.getId());//设置外籍外键id 就是套餐id

                //根据分类id展示菜品数据
                List<Dish>dishs=setmealDishMapper.getDisdById(dto.getCategoryId());
                for (Dish dish : dishs) {
                    //添加菜品到套餐里
                    setmealDishMapper.addSetmeadDish(dish.getId());//参数是集合

                }
            }


        }

    }


    /**
     * 设置起售停售套餐
     *
     * @param status
     */
    @Override
    public void updateStatus(Integer status, Integer id) {
        log.info("旧参数{},id是{}", status, id);
        setmealMapper.updateStatus(status, id);
    }

    /**
     * 分页查询
     *
     * @param
     * @return
     */
    @Override
    public PageResult get(SetmealPageQueryDTO pageQueryDTO) {

        //调用分页 插件 获取分页数据
        PageHelper.startPage(pageQueryDTO.getPage(), pageQueryDTO.getPageSize());
        //执行sql语句  返回套餐的集合
        log.info("分页{}，总分页{}", pageQueryDTO.getPage(), pageQueryDTO.getPageSize());

        Page<Setmeal> pageList = setmealMapper.get(pageQueryDTO);
        log.info("访问层返回得到的数据集合{}", pageList);

        //封装 返回
        PageResult pageResult = new PageResult (pageList.getTotal(), pageList.getResult());
        log.info("封装{}，和集合{}", pageList.getTotal(), pageList.getResult());
        return pageResult;
    }

    /**
     * 根据id批量删除套餐
     * @param ids
     */
    @Override
    @Transactional
    public void deleteById(List<Long> ids) {
        Long countIds = setmealMapper.countStatusByIds(ids);
        log.info("统计起售中的套餐有{}个", countIds);
        if (countIds > 0) {
            throw new DeletionNotAllowedException("套餐有启售中的，不能删除");
        } else if (CollectionUtils.isEmpty(ids))
            setmealMapper.deleteById(ids);

    }

    /**
     * 根据id查套餐
     *
     * @param id
     * @return
     */
    @Override
    public SetmealVO getById(Long id) {
        SetmealVO setmealVO = setmealMapper.getById(id);
        return setmealVO;
    }

    /**
     * 修改套餐
     *
     * @param setmealDTO
     */
    @Override
    public void upda(SetmealDTO setmealDTO) {

        //修改套餐
        setmealMapper.upda(setmealDTO);
        log.info("准备根据请求数据{}删除套餐关系", setmealDTO.getId());
        //根据id删除套餐关系
        setmealDishMapper.romveSD(setmealDTO.getId());

        //更新套餐关系（新增）
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealDTO.getId());//设置逻辑外键
            log.info("设置外键", setmealDTO.getId());
        }
        log.info("更新套餐关系的请求数据{}", setmealDishes);
        setmealDishMapper.upde(setmealDishes);
    }


    /**
     *    ↓    以下是：C端
     */


    /**
     * C端根据分类id查询套餐信息
     * @param categoryId
     * @return
     */
    @Override
    @CachePut(value = "Setmeal", key = "categoryId")
    public List<Setmeal> getSetmeal(Long categoryId) {
        log.info("c端根据分类id查询套餐信息{}",categoryId);
        List<Setmeal>setmealList= setmealMapper.getSetmeal(categoryId);
        return setmealList;
    }

}
